Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Remote Method Invocation is a way that a programmer makes use of Java programming language and its development environment remotely. Its all about how the objects on different computers interact in a distributed network. In this article on Remote Method Invocation in Java, I will tell you how to create an RMI application across Client and Server.
Below topics are covered in this article:
Let’s get started!
The RMI (Remote Method Invocation) is an API that provides a mechanism to create a distributed application in Java. The RMI allows an object to invoke methods on an object running in another JVM. The Remote Method Invocation provides remote communication between the applications using two objects stub and skeleton.
The stub object on the client machine builds an information block and sends this information to the server. The block consists of:
Skeleton Object
The skeleton object passes the request from the stub object to the remote object. It performs the following tasks:
It calls the desired method on the real object present on the server.
It forwards the parameters received from the stub object to the method.
With this, let move further and see how to create an RMI Application
Below steps helps you to create RMI Application:
Now, let’s get into the details of these steps.
The first thing that we have to do is to create an interface. This will provide a description of the methods that can be invoked by remote clients. This interface should extend the Remote interface and the method prototype within the interface should throw the RemoteException.
// Creating a Search interface import java.rmi.*; public interface Search extends Remote{ // Declaring the method prototype public String Query(String search) throws RemoteException; }
The next step is to implement the remote interface. In order to implement the remote interface, the class should extend to the UnicastRemoteObject class of java.rmi package. Also, a default constructor needs to be created to throw the java.rmi.RemoteException from its parent constructor.
// Java program to implement the Search interface import java.rmi.*; import java.rmi.server.*; public class SearchQuery extends UnicastRemoteObject implements Search{ // Default constructor to throw RemoteException from its parent constructor SearchQuery() throws RemoteException{ super(); }// Implementation of the query interface public String query(String search) throws RemoteException{ String result; if (search.equals("Reflection in Java")) result = "true"; else result = "false"; return result; } }
Step 3: Creating Stub and Skeleton objects from the implementation class using rmic
The RMIC tool is used to invoke the RMI compiler that creates the Stub and Skeleton objects. Its prototype is RMIC class name.
STEP 4: Start the RMIregistry
You need to start the registry service by issuing the command at the command prompt start RMIregistry
STEP 5: Create and execute the server application program
The next step is to create the server application program and execute it on a separate command prompt.
The server program uses the createRegistry method of LocateRegistry class to create rmiregistry within the server JVM with the port number passed as an argument.
The rebind method of Naming class is used to bind the remote object to the new name.
//program for server application import java.rmi.*; import java.rmi.registry.*; public class SearchServer{ public static void main(String args[]){ try{ // Create an object of the interface implementation class Search obj = new SearchQuery(); // rmiregistry within the server JVM with // port number 1900 LocateRegistry.createRegistry(1900); <p style="text-align: justify;">// Binds the remote object by the name //edureka Naming.rebind("rmi://localhost:1900"+ "/edureka",obj); } catch(Exception ae){ System.out.println(ae); } } }
Step 6: Create and execute the Client Application program
The last step is to create the Client Application program and execute it on a separate command prompt. The lookup method of Naming class is used to get the reference of the Stub object
The above client and server program is executed on the same machine and that’s why localhost is being used. In order to access the remote object from another machine, localhost is to be replaced with the IP address where the remote object is present.
So this brings us to the end of the RMI in Java article. I hope you found it informative and helped you in understanding the Fundamentals.
Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “RMI in Java ” article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co